#include "assettreeeditor.h"
#include "ui_assettreeeditor.h"
#include <QAbstractTableModel>


//////////////////////////////////////////////////////////////////////////
//
class FileTreeItem
{
public:
	typedef enum class Type : int
	{
		kDirectory,
		kFile
	};

	typedef QSharedPointer< FileTreeItem >			ItemPtr_t;
	typedef QVector< ItemPtr_t >					ItemVector_t;


	FileTreeItem()
	:	mType( Type::kDirectory )
	,	mName( "root" )
	{
	}

	FileTreeItem( Type type, const QString& name, FileTreeItem* parent=nullptr )
	:	mType( type )
	,	mName( name )
	,	mParent( parent )
	{
	}

	Type			type() const										{return mType;}
	void			type( Type t )										{mType = t;}
	QString			name() const										{return mName;}
	void			name( const QString& n )							{mName = n;}
	ItemPtr_t		parent() const										{return mParent;}
	ItemVector_t&	children()											{return mChildren;}
	int				childCount() const									{return mChildren.size();}

private:
	//
	Type			mType;
	QString			mName;
	ItemPtr_t		mParent;
	ItemVector_t	mChildren;
};


//////////////////////////////////////////////////////////////////////////
//
class FileSystemTree : public QAbstractItemModel
{
public:
	//////////////////////////////////////////////////////////////////////////
	// Column headers.
	static const char*				sColumnNames[];


	//////////////////////////////////////////////////////////////////////////
	// Custom interface.
	void							reset()
	{
		mRoot	=	FileTreeItem();
	}

	//////////////////////////////////////////////////////////////////////////
	// Required overrides.
	virtual QModelIndex				index( int row, int column, const QModelIndex& parent ) const override
	{
		if( row==0 )
			return createIndex( row, column, (void*)&mRoot );

		return createIndex( row, column, nullptr );
	}

	virtual QModelIndex				parent( const QModelIndex& child ) const override
	{
		return QModelIndex();
	}

	virtual int						rowCount( const QModelIndex& parent ) const override
	{
		if( parent.isValid() )
			return ((FileTreeItem*)parent.internalPointer())->childCount();

		// We always have a root.
		return 1;
	}

	virtual int						columnCount( const QModelIndex& parent ) const override
	{
		return 3;
	}

	virtual QVariant				data( const QModelIndex& index, int role ) const override
	{
		if( role!=Qt::DisplayRole )
			return QVariant();

		if( index.isValid() )
		{
			if( index.row()==0 )
			{
				switch( index.column() )
				{
				case 0:
					return ((FileTreeItem*)index.internalPointer())->name();
				case 1:
					return false;
				}
			}
		}

		return tr( "----" );
	}


	//////////////////////////////////////////////////////////////////////////
	// Custom overrides.
	Qt::ItemFlags					flags( const QModelIndex& index ) const override
	{
		return Qt::ItemIsEnabled;
	}

	virtual QVariant				headerData( int section, Qt::Orientation orientation, int role ) const override
	{
		if( role == Qt::DisplayRole )
			return QVariant( sColumnNames[ section ] );
		return QVariant();
	}

private:
	//////////////////////////////////////////////////////////////////////////
	FileTreeItem					mRoot;
};


//////////////////////////////////////////////////////////////////////////
const char*	FileSystemTree::sColumnNames[]	=
{
	"Path",
	"Overriden",
	"Name"
};



//////////////////////////////////////////////////////////////////////////
AssetTreeEditor::AssetTreeEditor(QWidget *parent) :
QWidget(parent),
ui(new Ui::AssetTreeEditor)
{
	ui->setupUi(this);

	// Initial population.
	ui->AssetTree->setModel( new FileSystemTree() );
	ui->AssetTree->header()->setStretchLastSection( true );
}

AssetTreeEditor::~AssetTreeEditor()
{
	delete ui;
}
